package com.gopay.cashier.web.controller;




import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gopay.cashier.common.exception.WebException;
import com.gopay.cashier.web.utils.CashierConfig;

@Controller
public class LogoutController extends BaseController {

    @RequestMapping("/logout.shtml")
    public void execute(HttpServletRequest request, HttpServletResponse response)  throws WebException {
        printRequest();
    	try {
			String cashierUrl = CashierConfig.get(CashierConfig.CASHIER_URL);
			StringBuffer sb = new StringBuffer();
			sb.append("/pay/order.shtml?");
			sb.append(GOPAY_ORDER_ID).append("=").append(getOrderId());
			if(getGopayUser()!=null){
				sb.append("&");
				sb.append(FIELD_U).append("=").append(getGopayUser());
			}
			
			SecurityUtils.getSubject().logout();
			
			String url = CashierConfig.get(CashierConfig.CAS_SERVER_URL) + "/logout?service=" + cashierUrl.concat(URLEncoder.encode(sb.toString(), "UTF-8"));
			
			WebUtils.issueRedirect(request, response,url);
		} catch (Exception e) {
			throw new WebException("");
		}
    }
}